using UnityEngine;
using QFramework;
using System;

namespace ProjectIndieFarm
{
    public partial class UIMessageItem : ViewController
    {
        public string Key;
        public string Message;
        public float Count;
        public string Endings;

        private IActionController mFadeAction;
        public void SetAlpha(float alpha)
        {
            Icon.ColorAlpha(alpha);
            TxtWithIcon.ColorAlpha(alpha);
            TxtWithOutIcon.ColorAlpha(alpha);
        }
        public void PopulateFields(string key, string message, float count, string endings)
        {
            Key = key;
            Message = message;
            Count = count;
            Endings = endings;
        }
        public void ShowIconText(Sprite icon, string message)
        {
            Icon.sprite = icon;
            TxtWithIcon.text = message;
            Icon.Show();
            TxtWithIcon.Show();
            TxtWithOutIcon.Hide();
            this.Show();
        }
        public void ShowOnlyText(string message)
        {
            TxtWithOutIcon.text = message;
            Icon.Hide();
            TxtWithIcon.Hide();
            TxtWithOutIcon.Show();
            this.Show();
        }
        public void UpdateIconText()
        {
            TxtWithIcon.text = $"{Message}{Count}{Endings}";
        }
        public void UpdateOnlyText()
        {
            TxtWithOutIcon.text = $"{Message}{Count}{Endings}";
        }

        public void StartFadeAction(Action onFinish)
        {
            if (mFadeAction != null)
            {
                mFadeAction.Deinit();
                mFadeAction = null;
            }
            mFadeAction = ActionKit.Sequence()
                .Callback(() => SetAlpha(1.0f))
                .Lerp(1.5f, 1.0f, 0.2f, scale => Icon.LocalScale(scale))
                .Delay(3.0f)
                .Lerp(1, 0, 2.0f, SetAlpha)
                .Start(this, () =>
                {
                    onFinish?.Invoke();
                    mFadeAction = null;
                });
        }
    }
}
